import * as actionTypes from './actionTypes';
import { fromJS } from 'immutable';
import axios from "axios";

const loginAction= (isLogin) => ({type: actionTypes.LOGIN, isLogin});

export const changeUsername = (username) => ({type: actionTypes.CHANGE_USERNAME, username: fromJS(username)});
export const changePassword = (password) => ({type: actionTypes.CHANGE_PASSWORD, password: fromJS(password)});
export const login = (username, password) => {
    return async (dispatch) => {
        try {
            const val = await axios.get("/mock/api/login.json?username=" + username + "&password=" + password);
            const data = val.data;
            if (data.success) {
                dispatch(loginAction(data.data));
            } else {
                console.error(data.msg);
            }
        } catch(e) {
            console.error("网络发送失败:" + e);
        }
    }
}

export const logout = () => ({type: actionTypes.LOGOUT});

